package com.sgb.kjwl.viewmodel.transport;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.CarInfo;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.CarriageSelectEntity;
import com.swgk.core.view.entity.DriverAuthDetailEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.transport.MapCarBiddingEntity;
import com.sgb.kjwl.model.entity.transport.MapLongAndLatEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.TransportRepertory;
import com.sgb.kjwl.view.ui.activity.transport.MapCarBiddingActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MapCarBiddingModel extends BaseViewModel {

    private MapCarBiddingActivity activity;
    private TransportRepertory repertory;

    public ObservableField<MapCarBiddingEntity> deTailEntity = new ObservableField<>(new MapCarBiddingEntity());

    public MapCarBiddingModel(MapCarBiddingActivity activity, TransportRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void driverTransList() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        Map map = new HashMap();
        map.put("page","1");
        map.put("limit","20");
        map.put("areacode",repertory.getAreaCode());
        APIManager.getInstance().getDriverAPI().getMapCarList(map).enqueue(new Callback<BaseEntity<ProListEntity<List<MapCarBiddingEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<MapCarBiddingEntity>>>> call, Response<BaseEntity<ProListEntity<List<MapCarBiddingEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.getTransInfo(response.body().getData().getList());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<MapCarBiddingEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //承运推送
    public void carrierPushCarrier() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().pushMapCarList(deTailEntity.get()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.finish();
                    MToast.showToast(activity, "推送成功");
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getUserInfo() {
        DialogHelper.showProgressDialog(activity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new Callback<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserMsgEntity>> call, Response<BaseEntity<UserMsgEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getData() == null) return;
                if (response.body().getState().equals("ok")) {
                    activity.displayRole(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //我的车型
    public void carType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().carType().enqueue(new Callback<BaseEntity<List<CarriageSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarriageSelectEntity>>> call, Response<BaseEntity<List<CarriageSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showCarModelPop(response.body().getData());
                } else {
                    MToast.showToast(activity.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarriageSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //我的车长
    public void carLenType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().carLenType().enqueue(new Callback<BaseEntity<List<CarriageSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarriageSelectEntity>>> call, Response<BaseEntity<List<CarriageSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showCarLengthPop(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarriageSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //发票要求
    public void invoiceType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().invoiceType().enqueue(new Callback<BaseEntity<List<CarriageSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarriageSelectEntity>>> call, Response<BaseEntity<List<CarriageSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showInvoicePop(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarriageSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getState().equals(APIManager.RESPONSE_OK))
                    activity.showAddressPop(response.body().getData());

            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }

    public void getCarInfo(){
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getCarInfo().enqueue(new Callback<BaseEntity<List<CarInfo>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarInfo>>> call, Response<BaseEntity<List<CarInfo>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showCarInfo(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarInfo>>> call, Throwable t) {

            }
        });
    }
    public void getCarInfo2(){
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getCarInfo().enqueue(new Callback<BaseEntity<List<CarInfo>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<CarInfo>>> call, Response<BaseEntity<List<CarInfo>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showCarInfo2(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<CarInfo>>> call, Throwable t) {

            }
        });
    }
    //是否自卸
    public void isDump() {
        CarriageSelectEntity one = new CarriageSelectEntity();
        CarriageSelectEntity two = new CarriageSelectEntity();
        List<CarriageSelectEntity> list = new ArrayList<>();
        one.setName("是");
        one.setNo("1");
        two.setName("否");
        two.setNo("0");
        list.add(one);
        list.add(two);
        activity.showIsDumpPop(list);
    }

    //付款方式
    public void getPayType(){
        APIManager.getInstance().getDriverAPI().getPayType().enqueue(new Callback<BaseEntity<List<GoodsInfoTypeEntity3>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Response<BaseEntity<List<GoodsInfoTypeEntity3>>> response) {
                if (response.body() == null) return;

                if (response.body().getState().equals("ok")) {
                    activity.showPayType(response.body().getData());
//                    SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.PAY_TYPE,new Gson().toJson(response.body().getData()));
                }
            }
            @Override
            public void onFailure(Call<BaseEntity<List<GoodsInfoTypeEntity3>>> call, Throwable t) {
            }
        });
    }
    //获取经纬度信息
    public void getLocationLL(String provinceNo,String cityNo,String districtNo,int type) {
        DialogHelper.showProgressDialog(activity, null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().getLocationLL(repertory.getAreaCode(), provinceNo, cityNo, districtNo).enqueue(new Callback<BaseEntity<MapLongAndLatEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MapLongAndLatEntity>> call, Response<BaseEntity<MapLongAndLatEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.getLocationLL(response.body().getData(),type);
                } else {
                    MToast.showToast(activity.getActivity(), response.message());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MapLongAndLatEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MToast.showToast(activity.getActivity(), t.getMessage());
            }
        });

    }
    //司机详情
    public void driverAuthDetail() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getDriverAPI().driverAuthDetail().enqueue(new Callback<BaseEntity<DriverAuthDetailEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<DriverAuthDetailEntity>> call, Response<BaseEntity<DriverAuthDetailEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.getDriverInfo(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<DriverAuthDetailEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
